package com.tianjin.weight.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

@Configuration
public class UploadFileConfig {

    @Value("${file.uploadFolderWin}")
    private String pathWin;

    @Value("${file.uploadFolderLinux}")
    private String pathLinux;

    @Value("${file.uploadFolderMac}")
    private String pathMac;

    @Bean
    MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();

        //判断是什么系统
        String os = System.getProperty("os.name");
        if(os.toLowerCase().startsWith("win")) {
            //path数据库保存的真实路径
            factory.setLocation(pathWin);
        }else if(os.toLowerCase().startsWith("mac")){
            //path数据库保存的真实路径
            factory.setLocation(pathMac);
        }else {
            //path数据库保存的真实路径
            factory.setLocation(pathLinux);
        }
        // 单次请求最大上传文件大小
        factory.setMaxRequestSize(DataSize.parse("10MB"));
        return factory.createMultipartConfig();
    }
}

